package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author jinze
 * @since 2022-08-07
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    //保存自媒体用户
    public ResponseResult<WmUser> saveMediaUser(WmUser user) {
        //从前端传过来user判断user信息是否为空
        if (user==null||user.getApUserId()==null|| StringUtils.isEmpty(user.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getApUserId,user.getApUserId());
        WmUser wmUser = this.getOne(queryWrapper);
        if (wmUser!=null){
            return ResponseResult.okResult(wmUser);
        }
        user.setCreatedTime(new Date());
        this.save(user);
        return ResponseResult.okResult(user);
    }
}
